package Thread.New.等待与唤醒;

import Method.Method;

public class BaoZiPu extends Thread{
    //先创建一个包子变量
    private BaoZi bz;

    public BaoZiPu(BaoZi baozi){
        this.bz = baozi;
    }

    @Override
    public void run() {
        int count = 0;
        //目的用于生产包子
        while(true){
            synchronized (bz){
                if(bz.flag){
                    try {
                        bz.wait();
                        //调用wait进入等待
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                }
                //这里是为了有2中包子
                if(count%2==0){
                    bz.pi = "薄皮";
                    bz.xian = "牛肉";
                }else{
                    bz.pi = "鱼皮";
                    bz.xian = "豆腐";
                }
                count++;
                System.out.println("生产包子ing,生产"+bz.pi+bz.xian+"包子");
                try {
                    Thread.sleep(3_000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                bz.flag = true;
                bz.notify();
                //唤醒顾客
                System.out.println("包子生产完毕");
            }
        }
    }
}
